package com.share.wechat.core.service.impl;

import com.share.common.tools.ID.IDutils;
import com.share.common.userinfo.UserUtil;
import com.share.wechat.core.entity.Detail;
import com.share.wechat.core.entity.Groom;
import com.share.wechat.core.entity.UserAction;
import com.share.wechat.core.repository.dao.DetailQueryDao;
import com.share.wechat.core.repository.inter.DetailRepository;
import com.share.wechat.core.service.DetailService;
import com.share.wechat.core.service.GroomService;
import com.share.wechat.core.service.UserActionService;
import com.share.wechat.core.service.UserDetailService;
import com.share.wechat.file.repository.inter.FileRepository;
import com.share.wechat.page.constant.PageConstant;
import com.share.wechat.page.help.PageHelper;
import com.share.wechat.page.help.PageUtil;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DetailServiceImpl implements DetailService {
    private Logger logger = Logger.getLogger(this.getClass());
    //商品的仓库类
    @Resource
    private DetailRepository detailRepository;
    //推荐服务类
    @Resource
    private GroomService groomService;
    //文件服务类
    @Resource
    private FileRepository fileRepository;
    //用户关注大类的推荐
    @Resource
    private UserActionService userActionService;
    @Resource
    private DetailQueryDao detailQueryDao;


    @Override
    public void save(Detail detail) {
        if (StringUtils.isEmpty(detail.getId())) {
            detail.setId(IDutils.getUUID());
        }
        detailRepository.save(detail);
        if (logger.isInfoEnabled()) {
            logger.info("插入对象" + detail);
        }
    }

    @Override
    public boolean check(String detailId) {
        if (logger.isInfoEnabled()) {
            logger.info("查询了" + detailId + "对应的对象是否存在");
        }
        return !StringUtils.isEmpty(detailRepository.findOne(detailId));
    }

    @Override
    public List<Detail> findByScore(int score, int page, int size) {
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        PageUtil pageUtil = PageHelper.getPageUtil(page, size, sort);
        Page<Detail> details = detailRepository.findByScore(score, pageUtil);
        return details.getContent();
    }

    @Override
    public List<Detail> findAll() {
        return (List<Detail>) detailRepository.findAll();
    }


    @Override
    public List<Detail> getDetailLoveBySort(int dataSort, int page, int size) {
        if (StringUtils.isEmpty(dataSort)) { //如果该大类的推荐渠道为空,则去专门的渠道去哪这个东西
            return this.getDetailLoveBySort(page, size);
        }

        Page<Groom> groomLists = groomService.getBySort(dataSort, page, size);
        if (CollectionUtils.isEmpty(groomLists.getContent())) {
            logger.info("未查询到有效数据");
            return null;
        }
        List<String> ids = new ArrayList<>(0);
        for (Groom groom : groomLists.getContent()) {
            ids.add(groom.getDetailId());
        }

        List<Detail> detailLists = detailRepository.findByIdIn(ids);
        if (CollectionUtils.isEmpty(detailLists)) {
            return null;
        }

        //把图片的数据设置进去
        for (Detail detail : detailLists) {
            detail.setImageDate(fileRepository.findOne(detail.getImageId()).getFileData());
        }
        return detailLists;
    }

    @Override
    public List<Detail> getDetailLoveBySort(int page, int size) {
        Page<Groom> scores = groomService.getByScores(page, size);
        List<Groom> scoresLists = scores.getContent();
        if (!CollectionUtils.isEmpty(scoresLists)) {
            List<String> ids = new ArrayList<>();
            for (Groom Groom : scoresLists) {
                ids.add(Groom.getId());
            }
            List<Detail> detailsList = detailRepository.findByIdIn(ids);
            //把图片的数据设置进去
            for (Detail detail : detailsList) {
                detail.setImageDate(fileRepository.findOne(detail.getImageId()).getFileData());
            }
            logger.info("查到的数据为:");
            logger.info(detailsList);
            return detailsList;
        }
        return null;
    }

    @Override
    public Detail showById(String id) {
        Detail detail = detailRepository.findOne(id);
        logger.info("查询到的数据为" + detail);
        return detail;


    }

    @Override
    public List<Detail> getDetailByUserInfo(String userInfo, int page, int size) {
        List<UserAction> actions = userActionService.getUserAction(userInfo, page, size);
        if (CollectionUtils.isEmpty(actions)) {//该用第一次使用 没有关注任何东西
            logger.debug("该用户未关注兴趣");
            return this.getDetailByNoGroom(page, size);
        } else {
            return this.getDetailByUserAction(actions);
        }
    }

    @Override
    public List<Detail> getDetailByNoGroom(int page, int size) {
        List<Groom> grooms = groomService.finTopGroom(page, size);
        List<String> ids = new ArrayList<>(0);
        for (Groom groom : grooms) {
            ids.add(groom.getDetailId());
        }
        List<Detail> returnList = detailRepository.findByIdIn(ids);
        logger.debug("用户未有兴趣点" + returnList);
        return returnList;
    }

    @Override
    public List<Detail> getDetailByUserAction(List<UserAction> actionList) {
        List<String> userLove = new ArrayList<>(0);

        for (UserAction action : actionList) {
            userLove.add(action.getGroomId());
        }

        List<Groom> groomList = groomService.findIds(userLove);
        logger.debug("查询到的结果为" + groomList);
        List<String> detailIds = new ArrayList<>(0);

        for (Groom groom : groomList) {
            detailIds.add(groom.getDetailId());
        }

        List<Detail> returnList = detailRepository.findByIdIn(detailIds);

        logger.debug("查询到的数据为" + returnList);
        return returnList;
    }

    //推荐 就纯看销量可以么 不知道
    // 并且该用户没有买过该商品
    @Override
    public List<Detail> getRecommendDetail(int page, int size) {
        String userInfo = UserUtil.getCurrentUserInfo();
        if (page < 0) {
            page = PageConstant.DEFAULT_INT_PAGE;
        }
        if (size < 0) {
            size = PageConstant.DEFAULT_INT_SIZE;
        }
        List<String> details = detailQueryDao.findRecommendDetails(userInfo, page, size);
        logger.debug("通过系统自动计算出的用户数据为");
        logger.debug(details);
        List<Detail> detailList = detailRepository.findByIdIn(details);
        logger.debug("所查询到的商品信息为" + detailList);
        return detailList;
    }
}
